package com.woniu.sharepark.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/** 实名验证工具类
 * @author DELL
 * @date 2019/6/4 11:52
 * version v1.0
 */
public class RealIdCard {

    public static String realId(String name,String idCard) throws IOException {

        String host = "https://idcardcert.market.alicloudapi.com";
        String path = "/idCardCert";
        String appcode = "a00fcd9d81e14070abab095d94241aaf";
        String urlSend = host + path + "?idCard=" + idCard + "&name=" + name;

        URL url = new URL(urlSend);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        //格式Authorization:APPCODE (中间是英文空格)
        httpURLConnection.setRequestProperty("Authorization", "APPCODE " + appcode);
        int httpCode = httpURLConnection.getResponseCode();
        String json = read(httpURLConnection.getInputStream());
        return json;
    }


    private static String read(InputStream is) throws IOException {
        StringBuffer sb = new StringBuffer();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        while ((line = br.readLine()) != null) {
            line = new String(line.getBytes(), "utf-8");
            sb.append(line);
        }
        br.close();
        return sb.toString();
    }



}
